/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package t4j.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import t4j.TBlogException;
import t4j.TBlogResponse;
import t4j.http.Response;
import t4j.org.json.JSONArray;
import t4j.org.json.JSONException;
import t4j.org.json.JSONObject;

/**
 * 
 * @author wangfuping
 * 
 * 用户信息
 *
 */
public class User extends TBlogResponse implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9031540340084915599L;

	/**
	 * 用户userid
	 */
	private String  userid;
	
	/**
	 *用户头像
	 */
    private String imageurl;
    
    /**
	 *用户昵称
	 */
    private String nickname;
    
    /**
	 *用户名/邮箱
	 */
    private String username;
    
    /**
	 *性别,男：1,女：0
	 */
    private String gender;
    
    /**
	 *介绍
	 */
    private String description;


	
	
	public User(Response res) throws TBlogException{
		init(res.asJSONObject());
	}
	
	public User(JSONObject json) throws TBlogException{
		init(json);
	}
	
	private void init(JSONObject json) throws TBlogException{
		
		try{
			this.userid = json.getString("userid");
			this.imageurl = json.getString("imageurl");
			this.nickname= json.getString("nickname");
			this.username = json.getString("username");
			this.gender = json.getString("gender");
			this.description = json.getString("description");
		} catch (JSONException je){
			throw new TBlogException(je.getMessage() + ":" + json.toString(), je);
		}
	}
	
	public static PagableResponseList<User> createPagableUserList(Response res) throws TBlogException {
        try {
            JSONObject json = res.asJSONObject();
            JSONArray list = json.getJSONArray("users");
            int size = list.length();
            PagableResponseList<User> users = new PagableResponseList<User>(size, json, res);
            for (int i = 0; i < size; i++) {
                users.add(new User(list.getJSONObject(i)));
            }
            return users;
        } catch (JSONException jsone) {
            throw new TBlogException(jsone);
        } catch (TBlogException te) {
            throw te;
        }
	}
	
	public static List<User> createUserList(JSONArray array) throws TBlogException {
		try{
			List<User> users = new ArrayList<User>();
			for(int i=0; i<array.length(); i++){
				users.add(new User(array.getJSONObject(i)));
			}
			return users;
		}catch(JSONException jsone){
			throw new TBlogException(jsone);
		}
	}
	
	public static List<User> createUserList(Response res) throws TBlogException{
		
		try{
			JSONArray list = res.asJSONArray();
			List<User> users = new ArrayList<User>();
			
			for(int i=0; i<list.length(); i++){
				users.add(new User(list.getJSONObject(i)));
			}
			return users;
		}catch(JSONException jsone){
			throw new TBlogException(jsone);
		}
	}
	
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getImageurl() {
		return imageurl;
	}
	public void setImageurl(String imageurl) {
		this.imageurl = imageurl;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}
